<?php
/**
 * 获得用户信息动作类
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0 GetUser add by yaoli<yaoli@pateo.com.cn> 
 */
class writeJson extends ActionBase {
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	public function action() {
		$db = new dbstuff ();
		$db->dbconnect ();
		// 获取路线信息
		
		$rqt = $this->getRoadbook ();
		
		if(isset($_REQUEST['dump'])){
		dump($rqt);	
		}
		$fontsize = $this->writeJson1 ( $rqt, 'json' );
		echo '<a href=' . $fontsize . ' target="_blank">压缩包</a>';
	
	}
	
	function getRoadbook() {
		global $_SGLOBAL;
		
		$formatarray = $array = array ();
		
		$sql = "select * from view_roadbook where FROM_UNIXTIME(update_time,'%Y%m%d%H') = FROM_UNIXTIME(unix_timestamp(),'%Y%m%d%H')";
		
		$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
		
		while ( $value = $_SGLOBAL ['db']->fetch_row () ) {
			$array [] = ($value);
		}
		
		foreach ( $array as $key => $value ) {
			
			$formatarray [$key] ['roadbook'] ['action'] = $value ['action'];
			$formatarray [$key] ['roadbook'] ['time'] = format_time ( $value ['update_time'] );
			
			
			$formatarray [$key] ['roadbook'] ['content'] = $value;
			
			$formatarray [$key] ['roadbook'] ['content'] ['depart'] = $this->getRouteWay ( $value ['depart'] );
			$formatarray [$key] ['roadbook'] ['content'] ['destination'] = $this->getRouteWay ( $value ['destination'] );
			$formatarray [$key] ['roadbook'] ['content'] ['rbId'] = $value ['roadbook_id'];
			$formatarray [$key] ['roadbook'] ['content'] ['rbName'] = $value ['roadbook_name'];
			
			
		
			
			$formatarray [$key] ['roadbook'] ['content'] ['passingList'] = $this->getRouteWay ( $value ['roadbook_id'] );
			$formatarray [$key] ['roadbook'] ['content'] ['poiList'] = $this->getRoutePoint ( $value ['roadbook_id'] );
			$formatarray [$key] ['roadbook'] ['content'] ['pictureList'] = $this->getPic ( $value ['roadbook_id'], 'roadbook' );
				
			
			unset ( $formatarray [$key] ['roadbook'] ['content'] ['roadbook_id'] );
			unset ( $formatarray [$key] ['roadbook'] ['content'] ['roadbook_name'] );
			unset ( $formatarray [$key] ['roadbook'] ['content'] ['action'] );
			unset ( $formatarray [$key] ['roadbook'] ['content'] ['update_time'] );
			unset ( $formatarray [$key] ['roadbook'] ['content'] ['create_time'] );
			
			
				
		
		
		}
		
		return $formatarray;
	}
	
	function getRoutePoint($route_id) {
		$formatarray = $array = array ();
		global $_SGLOBAL;
		$pointsql = "select vp.* from view_point vp right join roadbook_point vrp on vp.point_id=vrp.point_id where vp.point_id and vrp.roadbook_id=" . $route_id.' limit 1';
		
		$query_offica1 = $_SGLOBAL ['db']->query ( $pointsql );
		
		while ( $value = $_SGLOBAL ['db']->fetch_row () ) {
			$array [] = $value;
		
		}
	
		if($array){	
		foreach ( $array as $key => $value ) {
			$formatarray [$key] ['action'] = $value ['action'];
			$formatarray [$key] ['time'] = format_time ( $value ['update_time'] );
			
			
			$formatarray [$key] ['content'] = $value;
			$formatarray [$key] ['content'] ['pictures'] = $this->getPic ( $value ['point_id'], 'point' );
			unset ( $formatarray [$key] ['content'] ['point_id'] );
			unset ( $formatarray [$key] ['content'] ['update_time'] );
			unset ( $formatarray [$key] ['content'] ['action'] );
		
		}
		
		
		return $formatarray[0];
		}
	}
	
	function getRouteWay($route_id) {
		$formatarray = $array = array ();
		global $_SGLOBAL;
		$waysql = "select vw.* from view_way vw right join roadbook_way vrw on vw.way_id=vrw.way_id where vw.way_id>0 and vrw.roadbook_id=" . $route_id.' limit 1';
		
		$query_offica1 = $_SGLOBAL ['db']->query ( $waysql );
		
		while ( $value = $_SGLOBAL ['db']->fetch_row () ) {
			$array [] = $value;
		
		}
		if ($array) {
			foreach ( $array as $key => $value ) {
				
				$formatarray [$key] ['action'] = $value ['action'];
				$formatarray [$key] ['time'] = format_time ( $value ['update_time'] );
				
				$formatarray [$key] ['content'] = $value;
				$formatarray [$key] ['content'] ['pictures'] = $this->getPic ( $value ['way_id'], 'way' );
				
				unset ( $formatarray [$key] ['content'] ['way_id'] );
				unset ( $formatarray [$key] ['content'] ['update_time'] );
				unset ( $formatarray [$key] ['content'] ['action'] );
			}
		
		return $formatarray[0];
		}
		
	}
	
	function getPic($id, $type = 'roadbook') {
		
		$array = array ();
		if (isset ( $id )) {
			global $_SGLOBAL;
			$pointsql = "select * from view_pic where type='$type' and id=" . $id;
			$query_offica1 = $_SGLOBAL ['db']->query ( $pointsql );
			
			while ( $value = $_SGLOBAL ['db']->fetch_row () ) {
				$value ['url'] = $_SERVER ['HTTP_HOST'] . $value ['url'];
				$array ['url'][] = $value ['url'];
			
			}
		}
		return $array;
	}
	
	public function writeJson1($arr, $type = 'json') {
		
		$js_str = json_encode ( $arr );
		
		$diypath = DATA_ROOT . 'json/';
		// 建目录，并赋0777权限
		if (! is_dir ( $diypath )) {
			mkdir ( $diypath, 0777, 1 );
		}
		$filename = $diypath . 'rbupdate_roadbook' . date ( 'YmdH' ) . '.' . $type;
		
		$file = fopen ( $filename, 'w+' );
		$writeSize = fwrite ( $file, $js_str );
		fclose ( $file );
		
		
		if ($writeSize > 0) {
			$zipPath = $diypath . 'rbupdate_roadbook' . date ( 'YmdH' ) . '.zip';
			$zipSize = $this->doZip ( $filename, $zipPath );
			echo $zipSize;
			return '/Data/json/rbupdate_roadbook' . date ( 'YmdH' ) . '.zip';
			exit ();
		}
		return 0;
	}
	
	function doZip($srcFile, $zipFile) {
		$tmpPath = DATA_ROOT . 'temp';
		load_libra ( 'zip' );
		$zip = new ZIP ( $tmpPath );
		return $zip->zip ( $srcFile, NULL, $zipFile );
	}
	// 成路径方法
	
	function iconPathBuild($uid) {
		$path = $uid % 4096;
		$hash_path = sprintf ( "/%02X/%02X/%02X", ($path >> 8) & 0xF, ($path >> 4) & 0xF, $path & 0xF );
		return $hash_path;
	}

}
